<template>
  <div class="modal-overlay" v-if="visible">
    <div class="modal">
      <div class="modal-header">
        <h3>修改学生信息</h3>
        <span class="close-button" @click="closeDialog">&times;</span>
      </div>
      <div class="modal-content">
        <form @submit.prevent="handleSubmit">
          <div class="form-group">
            <label for="name">学生姓名:</label>
            <input type="text" id="name" v-model="studentForm.name" />
          </div>
          <div class="form-group">
            <label for="password">密码:</label>
            <input type="text" id="password" v-model="studentForm.password" />
          </div>
          <div class="form-group">
            <label for="activityLevel">课堂活跃程度:</label>
            <input type="number" id="activityLevel" v-model.number="studentForm.activityLevel" />
          </div>
          <div class="form-group">
            <label for="score">成绩:</label>
            <input type="number" id="score" v-model.number="studentForm.score" />
          </div>
          <div class="form-group">
            <label for="studyStatus">学习情况:</label>
            <input type="text" id="studyStatus" v-model="studentForm.studyStatus" />
          </div>
          <div class="form-actions">
            <button type="submit" class="submit-button">保存</button>
            <button type="button" class="cancel-button" @click="closeDialog">取消</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue';
import { StudentUpdate } from '../types/student';

export default defineComponent({
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    student: {
      type: Object,
      required: true
    }
  },
  emits: ['update:visible', 'update:student'],
  setup(props, { emit }) {
    const studentForm = ref<StudentUpdate>({});

    watch(() => props.student, (newStudent) => {
      if (newStudent) {
        studentForm.value = {
          name: newStudent.name,
          password: newStudent.password,
          activityLevel: newStudent.activityLevel,
          score: newStudent.score,
          studyStatus: newStudent.studyStatus
        };
      }
    }, { immediate: true });

    const closeDialog = () => {
      emit('update:visible', false);
      studentForm.value = {};
    };

    const handleSubmit = () => {
      emit('update:student', studentForm.value);
      closeDialog();
    };

    return {
      studentForm,
      closeDialog,
      handleSubmit
    };
  }
});
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background-color: white;
  width: 500px;
  border-radius: 5px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
}

.close-button {
  font-size: 24px;
  cursor: pointer;
}

.modal-content {
  padding: 20px;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

.form-group input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.submit-button, .cancel-button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.submit-button {
  background-color: #4CAF50;
  color: white;
}

.cancel-button {
  background-color: #f44336;
  color: white;
}

.submit-button:hover {
  background-color: #45a049;
}

.cancel-button:hover {
  background-color: #d32f2f;
}
</style>
